import router from '../router'
import {useRoute} from 'vue-router'
import {toRefs} from 'vue'
import {md5} from 'js-md5'

// 路由跳转
const go = (name, data = {}) => {
    router.push({name: name, query: data})
}
// 路由信息
const getRouter = () => {
    const route = useRoute()
    let r = toRefs(route)
    return {query: r.query.value, name: r.name.value}
}
// 静态资源路径
const getPath = (urlStr) => {
    return new URL('../assets' + urlStr, import.meta.url).href
}
// 获取URL参数
const getQueryParam = (url, paramName) => {
    // 获取当前页面的URL
    //var url = window.location.href;

    // 使用正则表达式匹配参数名和值
    const regex = new RegExp('[?&]' + paramName + '=([^&#]*)');
    const results = regex.exec(url);

    // 如果找到匹配项，返回对应的值；否则返回null
    if (results === null) {
        return null;
    } else {
        return decodeURIComponent(results[1].replace(/\+/g, ' '));
    }
}

const api = {
    // API地址前缀
    apiRoot: window.appConfig.apiRoot,//"http://192.168.2.16:8082",
    setToken: (token) => {
        localStorage.setItem('token', token)
    },
    getToken: () => {
        return localStorage.getItem("token") || null
    },
    setMID: (mid) => {
        localStorage.setItem('mid', mid)
    },
    getMID: () => {
        return localStorage.getItem("mid") || null
    },
    // 获取签名数据
    getSign: () => {
        const time = Math.floor(Date.now() / 1000);
        const sign = (md5('NoobMaster17359' + time.toString())).substring(4, 24)
        //return 'time=' + time + '&sign=' + sign
        return {time: time, sign: sign, mid: api.getMID()}
    },
    // 设备注册专用接口
    // deviceRegister: (mid) => {
    //     //mid='ef814f3e7b96-4d8b-a7f8-fef4acba86e0'
    //     const auth = api.getSign()
    //     return new Promise((resolve, reject) => {
    //         const opt = {
    //             method: 'POST',
    //             headers: {'Content-Type': 'application/json'},
    //             body: JSON.stringify({sign: auth.sign, time: auth.time, mid: mid})
    //         };
    //         fetch(api.apiRoot + 'mapi/login?time=' + auth.time + '&sign=' + auth.sign, opt).then(e => {
    //             e.json().then(e => {
    //                 resolve(e)
    //             }).catch(e => {
    //                 resolve(e)
    //             })
    //         })
    //     })
    // },
    // 公用邮差
    poster: (url, method = "POST", data = {}) => {
        let auth = api.getSign()
        let opt = {
            method: method,
            headers: {
                'authorization': api.getToken(),
                'Content-Type': 'application/json'
            }
        }
        data.time = auth.time
        data.sign = auth.sign
        data.mid = auth.mid
        if (method === "POST") {
            opt.body = JSON.stringify(data)
        }
        return new Promise((resolve, reject) => {
            fetch(api.apiRoot + url, opt).then(e => {
                e.json().then(e => {
                    resolve(e)
                }).catch(e => {
                    resolve(e)
                })
            })
        })
    }
}
export default {go, getRouter, getPath, api, getQueryParam}